package com.zkthink.ceres.cms.service.impl;

import static com.zkthink.ceres.common.constant.CacheKey.CMS_TEMPLATE;
import static com.zkthink.ceres.common.constant.CacheKey.buildTenantKey;

import cn.hutool.core.util.StrUtil;
import com.zkthink.base.service.SuperCacheServiceImpl;
import com.zkthink.ceres.cms.dao.CmsTemplateMapper;
import com.zkthink.ceres.cms.dto.CmsTemplateUpdateDTO;
import com.zkthink.ceres.cms.entity.CmsTemplate;
import com.zkthink.ceres.cms.service.CmsTemplateService;
import com.zkthink.ceres.common.enums.StoreDailyStatisticEnum;
import com.zkthink.ceres.store.service.StoreService;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.utils.BeanPlusUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.oschina.j2cache.CacheObject;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 业务实现类 装修配置表
 * </p>
 *
 * @author kellen
 * @date 2020-05-16
 */
@Slf4j
@Service
@AllArgsConstructor
public class CmsTemplateServiceImpl extends
    SuperCacheServiceImpl<CmsTemplateMapper, CmsTemplate> implements CmsTemplateService {


  private StoreService storeService;

  @Override
  protected String getRegion() {
    return CMS_TEMPLATE;
  }


  @Override
  public CmsTemplate getCmsTemplate(String tenantCode) {
    BaseContextHandler.setTenant(tenantCode);
    String key = buildTenantKey();
    CacheObject cacheObject = cacheChannel.get(getRegion(), key,
        (x) -> super.getOne(Wraps.<CmsTemplate>lbQ().eq(CmsTemplate::getIsCustom, false)));
    return (CmsTemplate) cacheObject.getValue();
  }

  @Override
  public CmsTemplate getCmsTemplate() {
    String key = buildTenantKey();
    CacheObject cacheObject = cacheChannel
        .get(getRegion(), key,
            (x) -> super.getOne(Wraps.<CmsTemplate>lbQ().eq(CmsTemplate::getIsCustom, false)));
    return (CmsTemplate) cacheObject.getValue();
  }

  @Override
  public void update(CmsTemplateUpdateDTO cmsTemplateUpdateDTO) {
    CmsTemplate cmsTemplate = BeanPlusUtil.copyProperties(cmsTemplateUpdateDTO, CmsTemplate.class);
    String key = buildTenantKey();
    cacheChannel.evict(getRegion(), key);
    cacheChannel.evict(getRegion(), key(cmsTemplateUpdateDTO.getId()));
    this.getBaseMapper().updateById(cmsTemplate);
  }

  @Override
  public CmsTemplate getAppCmsTemplate(String tenantCode) {
    BaseContextHandler.setTenant(tenantCode);
    String key = buildTenantKey();
    CacheObject cacheObject = cacheChannel.get(getRegion(), key,
        (x) -> this.getBaseMapper().getAppCmsTemplate(tenantCode));
    return (CmsTemplate) cacheObject.getValue();
  }

  @Override
  public CmsTemplate getStoreTemplate(Long storeId) {
    //埋点记录店铺访问人次
    String tenantCode = storeService.findTenantCodeByStoreId(storeId);
    if (StrUtil.isBlank(tenantCode)) {
      return null;
    }
    BaseContextHandler.setTenant(tenantCode);
    String key = buildTenantKey();
    CacheObject cacheObject = cacheChannel.get(getRegion(), key,
        (x) -> this.getBaseMapper().getAppCmsTemplate(tenantCode));
    storeService.addStorePoint(storeId, StoreDailyStatisticEnum.PV);
    return (CmsTemplate) cacheObject.getValue();
  }

  @Override
  public boolean isHasAdd() {
    return this.count(Wraps.<CmsTemplate>lbQ().eq(CmsTemplate::getIsCustom, false)) > 0;
  }
}
